package com.wuseguang.springcloud.controller;


import com.wuseguang.springcloud.Filter.RequestParameterWrapper;
import com.wuseguang.springcloud.service.RoleDataPermissionsService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 角色数据权限 前端控制器
 * </p>
 *
 * @author 
 * @since 2019-12-18
 */
@RestController
@RequestMapping("/roles")
public class RoleDataPermissionsController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private RoleDataPermissionsService roleDataPermissionsServiceImpl;

    /**
     * 查询角色的数据权限
     * @param request
     * @param roleId 角色的id
     * @return
     */
    @RequestMapping("/findRoleDataPermissions")
    public Result findRoleDataPermissions(RequestParameterWrapper request,
                                          @RequestParam(value = "roleId", required = false) Integer roleId){
        logger.info("查询角色的数据权限 RoleDataPermissionsController -> findRoleDataPermissions");

        //判断角色是否为空
        if (roleId == null) {
            return new Result(CodeUtil.REQUIRED_VALUE_IS_EMPTY.getCode(), CodeUtil.REQUIRED_VALUE_IS_EMPTY.getMsg() + "[角色为空]");
        }
        //获取学校id
        String  schoolId = request.getParam("schoolId");

        return roleDataPermissionsServiceImpl.findRoleDataPermissions(schoolId,roleId);
    }

    /**
     * 添加或者修改角色数据权限
     * @param request
     * @param roleId 角色id
     * @param dpid 数据权限id
     * @return
     */
    @RequestMapping("/addAndUpdateRoleDataPermissions")
    public Result addAndUpdateRoleDataPermissions(RequestParameterWrapper request,
                                                  @RequestParam(value = "roleId", required = false) Integer roleId,
                                                  @RequestParam(value = "dpid",required = false) Integer dpid){
        //判断角色是否为空
        if (roleId == null) {
            return new Result(CodeUtil.REQUIRED_VALUE_IS_EMPTY.getCode(), CodeUtil.REQUIRED_VALUE_IS_EMPTY.getMsg() + "[角色为空]");
        }
        //判断数据权限id是否为空
        if (dpid == null) {
            return new Result(CodeUtil.REQUIRED_VALUE_IS_EMPTY.getCode(), CodeUtil.REQUIRED_VALUE_IS_EMPTY.getMsg() + "[数据权限id为空]");
        }

        //获取学校id
        String  schoolId = request.getParam("schoolId");

        return roleDataPermissionsServiceImpl.addAndUpdateRoleDataPermissions(schoolId,roleId,dpid);
    }

}

